//
//  PageTitleView.swift
//  斗鱼直播Swift
//
//  Created by admin on 2018/2/24.
//  Copyright © 2018年 admin. All rights reserved.
//

import UIKit
protocol pageTitleViewDelegate : class{
    func pageTitleView(titleView:PageTitleView,index:Int)
}

private let kLineHeight : CGFloat = 2
class PageTitleView: UIView {
    private var titles:[String]
    private var index : Int = 0
    weak var delegate : pageTitleViewDelegate?
    lazy var scrollView : UIScrollView = {
       let scroll = UIScrollView.init()
        scroll.scrollsToTop = false
        scroll.bounces = false
        return scroll
    }()
//    var indicateLale : UILabel
    
    private lazy var scrollLine : UIView = {
       let line = UIView.init()
        line.backgroundColor = UIColor.orange
        return line
    }()
    //存放lalbe的数组
    private lazy var titleLables : [UILabel] = [UILabel]()
    
     init(frame: CGRect,titles:[String]) {
        self.titles = titles
        super.init(frame: frame)
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}

extension PageTitleView {
    private func setupUI (){
        scrollView.frame = bounds
        addSubview(scrollView)
        setupTitleLables()
        setupLine()
    }
    //标题
    private func setupTitleLables() {
        let lableW : CGFloat = frame.width / CGFloat(titles.count)
        let lableH : CGFloat = frame.height - kLineHeight
        let lableY : CGFloat = 0
        for (index,title) in titles.enumerated() {
            //创建标题
            let lable = UILabel.init()
            lable.text = title
            lable.tag = index
            lable.font = UIFont.systemFont(ofSize: 15)
            lable.textColor = UIColor.darkGray
            lable.textAlignment = .center
            //尺寸
            let lableX : CGFloat = lableW * CGFloat(index)
            lable.frame = CGRect.init(x: lableX, y: lableY, width: lableW, height: lableH)
            lable.isUserInteractionEnabled = true
            lable.addGestureRecognizer(UITapGestureRecognizer.init(target: self, action: #selector(didChangeCick(tap:))))
            titleLables.append(lable)
            scrollView.addSubview(lable)
            
        }
    }
    private func setupLine() {
        let bottomLine = UIView.init()
        
        bottomLine.backgroundColor = UIColor.lightGray
        bottomLine.frame = CGRect.init(x: 0, y: frame.height - 0.5, width: kScreenW, height: 0.5)
        addSubview(bottomLine)
        let lable = titleLables.first
        scrollLine.frame = CGRect.init(x: (lable?.frame.origin.x)!, y:(lable?.frame.maxY)!, width: (lable?.frame.width)!, height: kLineHeight)
        scrollView.addSubview(scrollLine)
        
    }
    
}

extension PageTitleView {
    @objc private func didChangeCick(tap:UITapGestureRecognizer) {
        guard let currentLable = tap.view as? UILabel else {
            return
        }
        currentLable.textColor = UIColor.orange
        
        let oldLable = titleLables[index]
        oldLable.textColor = UIColor.darkGray
        index = currentLable.tag
        let scrollLineX = CGFloat (index) * currentLable.frame.width
        UIView.animate(withDuration: 0.15) {
            self.scrollLine.frame.origin.x = scrollLineX
        }
        
        delegate?.pageTitleView(titleView: self, index: index)
    }
}

extension PageTitleView {
    func setTitleViewProgress(progress:CGFloat,sourceIndex:Int,targetIndex:Int)  {
//        let sourceLable = titleLables[sourceIndex]
//        targetIndex
    }
}
